package com.unigator;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
<<<<<<< HEAD
import java.util.Map.Entry;
=======
>>>>>>> 9a456f07899b24566de210037bc575d047e37e32

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.json.JSONArray;
import org.json.JSONObject;

<<<<<<< HEAD
=======
import com.unigator.RecordPath.transData;
>>>>>>> 9a456f07899b24566de210037bc575d047e37e32
import com.unigator.ServerIO.eFunc;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

 
public class MainActivity extends Activity 
{
	
<<<<<<< HEAD
	HashMap<String,Integer> pointList;
	ArrayAdapter<String> srcAdapter;
	ArrayAdapter<String> dstAdapter;
	String text = "";
	ListView srcList;
	ListView dstList;
	EditText editSrc;
	EditText editDst;
	int srcId;
	int dstId;
		
	
=======
>>>>>>> 9a456f07899b24566de210037bc575d047e37e32
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
<<<<<<< HEAD
    

        pointList = new HashMap<String,Integer>();
        
        //transData.execute(eFunc.FindPointByName,"C");
        
        //get the text field for the src location
        editSrc = (EditText)findViewById(R.id.edit_src);
        //get the text field for the dst location
        editDst = (EditText)findViewById(R.id.edit_dst);
        
        //create adapter for the text fields
        srcAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
        dstAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
        
        //set the adapter
        srcList = (ListView)findViewById(R.id.src_list_view);
        srcList.setAdapter(srcAdapter);
        dstList = (ListView)findViewById(R.id.dst_list_view);
        dstList.setAdapter(dstAdapter);
        
        
        editSrc.addTextChangedListener(new TextWatcher(){
		public void afterTextChanged(Editable s) {
			
			if(s.toString().contains(" "))
			{
				return;
			}
			text= "src";  
			new transData().execute(eFunc.FindPointByName,s.toString());
		  }
		  public void beforeTextChanged(CharSequence s, int start, int count, int after){}
		  public void onTextChanged(CharSequence s, int start, int before, int count){}
		    });
    	
        editDst.addTextChangedListener(new TextWatcher(){
		public void afterTextChanged(Editable s) {
			
			if(s.toString().contains(" "))
			{
				return;
			}
			text= "dst";
			new transData().execute(eFunc.FindPointByName,s.toString());
		  }
		  public void beforeTextChanged(CharSequence s, int start, int count, int after){}
		  public void onTextChanged(CharSequence s, int start, int before, int count){}
		    });
=======
       
        new transData().execute(eFunc.FindPathInt,1,6);
        new transData().execute(eFunc.FindPointByName,"CS");
>>>>>>> 9a456f07899b24566de210037bc575d047e37e32
        
    }
    
    public void sendMessage(View view) 
    {
<<<<<<< HEAD
        Intent intent = new Intent(this, Map.class);
        intent.putExtra("src_id", srcId);
        intent.putExtra("dst_id", dstId);
        startActivity(intent);
    }
    
    
=======
		Intent intent = new Intent(this, Map.class);
	    startActivity(intent);
    }
    
>>>>>>> 9a456f07899b24566de210037bc575d047e37e32
    public class transData extends ServerIO
    {
    	transData()
    	{
    		super();
    	}
    	
    	@Override
    	protected void onPostExecute(Object object)
    	{
    		RetObject retObject=(RetObject)object;
    		eFunc func=retObject.func;
    		switch(func)
    		{
<<<<<<< HEAD
    			case FindPointByName:
    			{
    				HashMap<String,Integer> map =(HashMap<String,Integer>)retObject.retData;
    				pointList.clear();
    				for (Entry<String, Integer> entry : map.entrySet()) {
    				    String key = entry.getKey();
    				    int value = entry.getValue();
    				    pointList.put(key, value);
    				}
    				
    				if(text.equalsIgnoreCase("src"))
    				{
    					srcAdapter.clear();
    					if(pointList.size() == 0)
    					{
    						srcAdapter.add("no result!");
    					}
    					else
    					{
    						for( String key : pointList.keySet() ) 
    						{
    							srcAdapter.add(key);
    						}
    					}
    					srcAdapter.notifyDataSetChanged();
    					
    			        srcList.setOnItemClickListener(new OnItemClickListener() {
    			            public void onItemClick(AdapterView<?> parent, View view, int position,
    			                    long id) {
    			                
    			                String item = srcList.getItemAtPosition(position).toString();
    			                editSrc.setText(item);
    			                srcAdapter.clear();
    			                srcId = pointList.get(item);
    			            }  
    			        });
    				}
    				else
    				{
    					dstAdapter.clear();
    					if(pointList.size() == 0)
    					{
    						dstAdapter.add("no result!");
    					}
    					else
    					{
    						for( String key : pointList.keySet() ) 
    						{
    							dstAdapter.add(key);
    						}
    					}
    					dstAdapter.notifyDataSetChanged();
    					
    			        
    			        dstList.setOnItemClickListener(new OnItemClickListener() {
    			            public void onItemClick(AdapterView<?> parent, View view, int position,
    			                    long id) {
    			                
    			                String item = dstList.getItemAtPosition(position).toString();
    			                editDst.setText(item);
    			                dstAdapter.clear();
    			                dstId = pointList.get(item);
    			            }  
    			        });
    				}
    				
=======
    			case FindPathInt:
    			{
    				ArrayList<MyLocation> locationList=(ArrayList<MyLocation>)retObject.retData;
    	    		MyLocation location=locationList.get(0);
    				break;
    			}
    			case FindPathDouble:
    			{
    				ArrayList<MyLocation> locationList=(ArrayList<MyLocation>)retObject.retData;
    	    		MyLocation location=locationList.get(0);
    				break;
    			}
    			case FindPointByName:
    			{
    				HashMap<String,Integer> pointList=(HashMap<String,Integer>)retObject.retData;
    	    		int id =pointList.get("CS 216");
    	    		System.out.println(id);
>>>>>>> 9a456f07899b24566de210037bc575d047e37e32
    				break;
    			}
    		}
    		
    		
    	}
    }

}

